<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button @click="addCateDialog">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 分级表格 -->
      <tree-table
        class="tree-table"
        :data="cateList"
        :columns="columns"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <!-- 是否有效 -->
        <template slot="isOK" slot-scope="scope">
          <i
            v-if="scope.row.cat_deleted === false"
            class="el-icon-circle-check"
            style="color: lightgreen; font-size: 20px;"
          ></i>
          <i v-else class="el-icon-circle-close" style="color: red; font-size: 20px;"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0" type="success">一级分类</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1">二级分类</el-tag>
          <el-tag v-else type="warning">三级分类</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="operate" slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top" :enterable="false">
            <el-button
              @click="showEditDialog(scope.row.id)"
              type="primary"
              size="mini"
              icon="el-icon-edit"
            ></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top" :enterable="false">
            <el-button
              @click="removeUser(scope.row.id)"
              type="danger"
              size="mini"
              icon="el-icon-delete"
            ></el-button>
          </el-tooltip>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 10, 20, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加商品分类对话框 -->
    <el-dialog
      @close="addCateDialogClose"
      title="添加商品分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
    >
      <!-- 内容 -->
      <el-form
        status-icon
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
            :options="paramsCateList"
            v-model="selectedKeys"
            :props="cascaderProps"
            @change="parentCateChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品分类数据列表
      cateList: [],
      // 总数据条数
      total: 0,
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 控制添加对话框的显示
      addCateDialogVisible: false,
      // 分类的数据
      addCateForm: {
        // 将要添加的分类名称
        cat_name: '',
        // 父级分类id
        cat_pid: 0,
        // 分类等级
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 定义table的列
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 将当前列定义为模板列
          type: 'template',
          // 模板名称
          template: 'isOK'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operate'
        }
      ],
      // 父级分类列表
      paramsCateList: [],
      // 指定配置对象
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
        // 多选选择任意一级选项
        multiple: true,
        checkStrictly: true
      },
      // 选中的父级分类数组
      selectedKeys: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取商品分类数据
    async getCateList() {
      const res = await this.$http.get('categories', { params: this.queryInfo })
      if (res.data.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }
      // console.log(res)
      this.cateList = res.data.data.result
      this.total = res.data.data.total
    },
    // 监听pagesize变化
    handleSizeChange(newSize) {
      // console.log('newSize', newSize)
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newPage) {
      // console.log('newPage', newPage)
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    // 对话框关闭事件
    addCateDialogClose() {
      this.$refs.editFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    },
    // 添加新分类
    addCate() {
      console.log(this.addCateForm)
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) {
          return
        }
        const res = await this.$http.post('categories', this.addCateForm)
        if (res.data.meta.status !== 201) {
        }
        this.$message.success('添加成功')
        this.getCateList()
        this.addCateDialogVisible = false
      })
    },
    // 获取父级分类数据
    async getParamsCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类失败')
      }
      this.paramsCateList = res.data
    },
    // 展示对话框
    addCateDialog() {
      this.getParamsCateList()
      this.addCateDialogVisible = true
    },
    // 选择项发生变化事件
    parentCateChange() {
      // selectedKeys大于0，有选择父级分类
      // console.log(this.selectedKeys)
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[
          this.selectedKeys.length - 1
        ]
        this.addCateForm.cat_level = this.selectedKeys.length
        // console.log(this.addCateForm.cat_pid)
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    }
  }
}
</script>

<style lang="less" scoped>
.tree-table {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
